package com.qf.smsplatform.webmaster.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.smsplatform.webmaster.cache.DirtywordCache;
import com.qf.smsplatform.webmaster.constans.ResultCode;
import com.qf.smsplatform.webmaster.events.UpdateDirtywordEvent;
import com.qf.smsplatform.webmaster.exceptions.MyBaseException;
import com.qf.smsplatform.webmaster.exceptions.UpdateDataException;
import com.qf.smsplatform.webmaster.mapper.DirtywordMapper;
import com.qf.smsplatform.webmaster.pojo.CheckType;
import com.qf.smsplatform.webmaster.pojo.TAdminUser;
import com.qf.smsplatform.webmaster.pojo.TDirtyword;
import com.qf.smsplatform.webmaster.service.DirtywordService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.List;

@Service
@Transactional
public class DirtywordServiceImpl implements DirtywordService {

    private DirtywordMapper dirtywordMapper;

    private DirtywordCache dirtywordCache;

    private ApplicationContext context;

    @Autowired
    public void setDirtywordCache(DirtywordCache dirtywordCache){
        this.dirtywordCache = dirtywordCache;
    }

    @Autowired
    public void setContext(ApplicationContext context){
        this.context = context;
    }

    @Autowired
    public void setDirtywordMapper(DirtywordMapper dirtywordMapper){
        this.dirtywordMapper = dirtywordMapper;
    }


    @Override
    public TDirtyword findDirtyById(Long id) {

        if (ObjectUtils.isEmpty(id) || id <= 0) {
            //抛一个自定义的异常
            throw new MyBaseException("传递的主键不合法", ResultCode.PK_NOT_ALLOW);
        }
        return dirtywordMapper.findDirtyById(id);
    }

    @Override
    public void addDirty(TDirtyword dirty) {
        if (dirty == null){
            throw new MyBaseException("敏感词不能为空", ResultCode.PK_NOT_ALLOW);
        }
        dirtywordMapper.addDirty(dirty);

    }

    @Override
    @Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
    public PageInfo<TDirtyword> selectAll(int pageNum, int pageSize) {
        PageHelper.startPage(pageNum, pageSize);//开始分页
        List<TDirtyword> dirtywordList = dirtywordMapper.selectAll();//查询数据
        PageInfo<TDirtyword> pageInfo = new PageInfo<>(dirtywordList);//封装数据
        return pageInfo;
    }

    @Override
    public void updateDirty(TDirtyword tDirtyword) {
        //先进行校验
        if (tDirtyword.isNull(CheckType.UPDATE)){
            throw new UpdateDataException("必须传递主键", ResultCode.DATA_ISNULL);
        }

        TDirtyword queryData =dirtywordMapper.findDirtyById(tDirtyword.getId());
        if (tDirtyword.getOwntype() != 1 && tDirtyword.getCreater() != queryData.getCreater()){
            throw new UpdateDataException("您没有权限更新当前数据", ResultCode.NO_PERMISSON);
        }
        dirtywordMapper.updateDirty(tDirtyword);

        context.publishEvent(new UpdateDirtywordEvent());
    }

    @Override
    public void deleteDirtyByIds(List<Integer> ids) {

        RequestAttributes requestAttributes = RequestContextHolder.currentRequestAttributes();
        Object user = ((ServletRequestAttributes) requestAttributes).getRequest().getSession().getAttribute("user");
        if (((TAdminUser) user).getType() == 1){
            dirtywordMapper.deleteDirtyByIds(ids);

        }else {
            dirtywordMapper.deleteDirtyByIdsAndCreater(ids, ((TAdminUser) user).getId());
        }
        context.publishEvent(new UpdateDirtywordEvent());
    }
}
